﻿#include "SdkMotionDetect.h"

#define DEF_TAG             "MD"


#if !CMAKE_USE_CHIP_AI

StSdkMDAreaLocal g_SdkMotionDetect_Area;
IMPIVSInterface *g_SdkMotiondetect_Interface = NULL;
int g_SdkMotiondetect_ChnNum = 2;

int ChipSdk_ivs_move_start(int grp_num, int chn_num, IMPIVSInterface **interface)
{
    int ret = 0;
    IMP_IVS_MoveParam param;

    memset(&param, 0, sizeof(IMP_IVS_MoveParam));
    param.skipFrameCnt = 1;
    param.frameInfo.width = SENSOR_SUB_WIDTH;
    param.frameInfo.height = SENSOR_SUB_HEIGHT;
    param.roiRectCnt = 1;
    param.sense[0] = 4;
    param.roiRect[0].p0.x = 0;
    param.roiRect[0].p0.y = 0;
    param.roiRect[0].p1.x = param.frameInfo.width - 1;
    param.roiRect[0].p1.y = param.frameInfo.height - 1;

    *interface = IMP_IVS_CreateMoveInterface(&param);
    if (*interface == NULL)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_CreateGroup(%d) failed\n", grp_num);
        return -1;
    }

    ret = IMP_IVS_CreateChn(chn_num, *interface);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_CreateChn(%d) failed\n", chn_num);
        return -1;
    }

    ret = IMP_IVS_RegisterChn(grp_num, chn_num);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_RegisterChn(%d, %d) failed\n", grp_num, chn_num);
        return -1;
    }

    ret = IMP_IVS_StartRecvPic(chn_num);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_StartRecvPic(%d) failed\n", chn_num);
        return -1;
    }

    return 0;
}

int Ext_SdkMotionDetect_Init(void)
{
    int ret;

    ret = ChipSdk_ivs_move_start(0, g_SdkMotiondetect_ChnNum, &g_SdkMotiondetect_Interface);
    if (ret < 0)
    {
        IMP_LOG_ERR(DEF_TAG, "sample_ivs_move_start(0, 0) failed\n");
        return -1;
    }

    return 0;
}

void Ext_SdkMotionDetect_Exit(void)
{
    int ret = 0;

    ret = IMP_IVS_StopRecvPic(g_SdkMotiondetect_ChnNum);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_StopRecvPic failed\n");
        return;
    }

    sleep(1);

    ret = IMP_IVS_UnRegisterChn(g_SdkMotiondetect_ChnNum);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_UnRegisterChn failed\n");
        return;
    }

    ret = IMP_IVS_DestroyChn(g_SdkMotiondetect_ChnNum);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_DestroyChn failed\n");
        return;
    }

    IMP_IVS_DestroyMoveInterface(g_SdkMotiondetect_Interface);
}

int Ext_SdkMotionDetect_SetSensitivity(int val)
{
    return 0;
}

void Ext_SdkMediaCom_GetMDAreaXY(pStSdkMDAreaLocal area)
{
    //area->l = g_SdkMotionDetect_Area.l;
    //area->r = g_SdkMotionDetect_Area.r;
    //area->t = g_SdkMotionDetect_Area.t;
    //area->b = g_SdkMotionDetect_Area.b;
}

int Ext_SdkMediaCom_GetMdResult(void)
{
    int ret = 0;
    IMP_IVS_MoveOutput *result = NULL;
    Com_s32 md_result[6] = {0};

    ret = IMP_IVS_PollingResult(g_SdkMotiondetect_ChnNum, IMP_IVS_DEFAULT_TIMEOUTMS);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_PollingResult failed\n");
        return DEF_COM_FALSE;
    }

    ret = IMP_IVS_GetResult(g_SdkMotiondetect_ChnNum, (void **)&result);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_GetResult failed\n");
        return DEF_COM_FALSE;
    }

    int i;
    for(i = 0; i < 4;i++)
    {
        md_result[i] = result->retRoi[i];
        //PRT_DBG(DEF_TAG,"retRoi[%d]: %d\n",i,result->retRoi[i]);
    }

    ret = IMP_IVS_ReleaseResult(g_SdkMotiondetect_ChnNum, (void *)result);
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "IMP_IVS_ReleaseResult failed\n");
        return DEF_COM_FALSE;
    }

    if(md_result[0] == 1)
    {
        //PRT_INFO(DEF_TAG, "montion detect is ok\n");
        return DEF_COM_TRUE;
    }

    return DEF_COM_FALSE;
}

#endif
